﻿/*
 * PsTFS is a set of PowerShell commands to manage and use Team Foundation Server. PowerShell 
 * effectiveness is combined with the power of TFS API to maximizes TFS capabilities.
 * 
 * Creator : Choulant Noham
 * Mail : choulant@gmail.com
 * Blog : http://choulant.blogspot.com
 * 
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management.Automation;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.VersionControl.Client;
using System.Collections;
using Microsoft.TeamFoundation.Server;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using PowerShell.Host.PsTFS.CdmLet.PsObject;

namespace PowerShell.Host.PsTFS.CdmLet
{
    /// <summary>
    /// Récupération d'un WorkItem
    /// </summary>
    [Cmdlet(VerbsCommon.Get, "Files")]
    public class Get_Files: Cmdlet
    {
        public Get_Files()
        {
            if (TFSProvider.s_TFSDriveInfo != null)
            {
                this.TFS = TFSProvider.s_TFSDriveInfo.TFS;
                this.Path = "$/" + TFSProvider.s_TFSDriveInfo.ProjectName != null ? TFSProvider.s_TFSDriveInfo.ProjectName : String.Empty;
            }
            this.RecursionType = RecursionType.Full;
        }
        protected override void ProcessRecord()
        {
            if (TFS == null)
            {
                WriteError(new ErrorRecord(new ArgumentException("No server TFS!!!"), "PsTFS", ErrorCategory.ObjectNotFound, TFS));
            }
            try
            {
                VersionControlServer vcs = PsService.VCS(this.TFS);
                ItemSet itemSet = vcs.GetItems(this.Path, this.RecursionType);
                BranchHistoryTreeItem[][] history = vcs.GetBranchHistory
                    (new ItemSpec[] { new ItemSpec(this.Path, RecursionType.None) },
                   VersionSpec.Latest);

                BranchHistoryTreeItem item = history[0][0].GetRequestedItem();

                //BranchHistoryTreeItem[][] branchHistory = vcs.GetBranchHistory(this.Path,VersionSpec.Latest);
                //if (branchHistory.Length > 0)
                //{
                //    ;
                //}

                //Retourne l'assembly compilée
                WriteObject(itemSet.Items);
                
            }
            catch (Exception ex)
            {
                ErrorRecord err = new ErrorRecord(ex, "<ID>", ErrorCategory.WriteError, null);
                WriteError(err);
                PsLog.AddLog(ex.StackTrace);
            }

        }

        /// <summary>
        /// Permet d'informer la fin de la commande
        /// </summary>
        protected override void EndProcessing()
        {
            PsLog.AddLog(null, "Get-Files is end");
            base.EndProcessing();
        }

        [Parameter(Position = 0)]
        public TeamFoundationServer TFS { get; set; }

        [Parameter(Position = 1)]
        public string Path { get; set; }

        [Parameter(Position = 2)]
        [ValidateNotNullOrEmpty]
        public RecursionType RecursionType { get; set; }
    }
}